package ui.swing.model;

import java.math.BigDecimal;
import java.util.LinkedHashSet;
import java.util.Random;

import javax.swing.table.DefaultTableModel;

import data.Constant;
import data.DataBuffer;
import data.LoadData;
import data.ReadAndWriteCareCode;
import data.TimeStock;

public class StockTableModel extends DefaultTableModel implements Stoppable{
	private static final long serialVersionUID = -4632602808372007121L;

	protected String[] col = new String[]{"code","name","curPrice","diffPrice","ratePercent","amount","bat","time","maxprice","minprice","industry","sinaindustry"
			,"priceSpeed"};
	
//	protected static String[] col = new String[]{"code","name","curPrice","diffPrice","ratePercent","bat","priceSpeed"};
	
	protected LinkedHashSet<String> codeSet = ReadAndWriteCareCode.getCodeSet();
	protected Object[][] datas = new Object[codeSet.size()][col.length];
	private int choice = 0;
	private boolean run = true;
	
	public StockTableModel(int chose) {
		super();
		loadData();
		choice = chose;
		setDataVector(datas,col);
		
		Thread th = new Thread(this);
		th.start();
	}

	public void stop(){
		run = false;
	}

	protected void loadData(){
		
		switch (choice) {
		case 0:
			codeSet = DataBuffer.getOwn();
			break;
		case 1:
			codeSet = ReadAndWriteCareCode.getCodeSet();
			break;
		case 2:
			codeSet = DataBuffer.getFinalSel();
			break;
		case 3:
			codeSet = DataBuffer.getIndustryOrder();
			break;
		case 4:
			codeSet = DataBuffer.getSinaIndustryOrder();
			break;
		case 5:
			codeSet = DataBuffer.getLowUP();
			break;
		case 6:
			codeSet = DataBuffer.getVerLine();
			break;
		case 7:
			codeSet = DataBuffer.getSpeedUp();
			break;
		case 8:
			codeSet = DataBuffer.getBigdeal();
			break;
		case 9:
			codeSet = DataBuffer.getUpline();
			break;
		default:
			codeSet = ReadAndWriteCareCode.getCodeSet();
			break;
		}
		
		datas = new Object[codeSet.size()][col.length];
		int i=0;
		synchronized (codeSet) {
			for (String code : codeSet) {
				TimeStock ts = LoadData.getTimeStock(code);
				/*if(ts==null){
					System.out.println(code);
					continue;
				}*/
				datas[i][0]=ts.getCode();
				datas[i][1]=ts.getStockName();
				datas[i][2]=ts.getCurPrice().toString();
				datas[i][3]=ts.getCurPrice().subtract(ts.getPreDayPrice())
						.toString();
				BigDecimal increPriceRate = ts.getRatePercent();
				if (increPriceRate.compareTo(Constant.ZERO) > 0) {
					datas[i][4] = "+" + increPriceRate + "%";
				} else {
					datas[i][4] = increPriceRate + "%";
				}
				
				datas[i][5] = ts.getAmount().toString();
				datas[i][6] = ts.getAmountBat().toString();
				datas[i][7] = ts.getTime().toString();
				datas[i][8] = ts.getMaxPrice().toString();
				datas[i][9] = ts.getMinPrice().toString();
				datas[i][10] = ts.getIndustry();
				datas[i][11] = ts.getSinaIndustry();
				datas[i][12]=ts.getPriceWive().toString();
				
				i++;
			}
		}
	}

	private void fillValues(){
		this.setRowCount(datas.length);
		for (int i = 0; i < datas.length; i++) {
			for (int j = 0; j < datas[i].length; j++) {
				setValueAt(datas[i][j], i, j);
			}
		}
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(run){
			try {
				loadData();
				fillValues();
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}
	
	
}
